package com.catmiao.spark.rdd.operator.transform

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @title: RDD_Operator_01_transform
 * @projectName spark_study
 * @description: TODO
 * @author ChengMiao
 * @date 2024/2/2 11:48
 */
object RDD_Operator_12_transform {

  def main(args: Array[String]): Unit = {
    val sparkCon = new SparkConf().setMaster("local[*]").setAppName("rdd")

    val sparkContext = new SparkContext(sparkCon)



    val rdd1 = sparkContext.makeRDD(List(1, 2, 3, 4))
    val rdd2 = sparkContext.makeRDD(List(3, 4, 5, 6))

    // 1. intersection 获取交集
    val rsRdd1 = rdd1.intersection(rdd2).collect()
    println(rsRdd1.mkString(","))

    // 2. union 获取并集
    val rsRdd2 = rdd1.union(rdd2).collect()
    println(rsRdd2.mkString(","))

    // 3. subtract 获取差集
    val rsRdd3 = rdd1.subtract(rdd2).collect()
    println(rsRdd3.mkString(","))

    // 4. 拉链 zip
    val rsRdd4 = rdd1.zip(rdd2).collect()
    println(rsRdd4.mkString(","))

    /**
     * 如果比较的数据源类型不一致 无法编译
     * 拉链要求数据源的长度一致，分区数量保持一致
     */

    sparkContext.stop()
  }

}
